package com.pubsys.main.server.rpc.login;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.pubsys.main.client.rpc.login.LoginService;
import com.pubsys.main.server.database.Database;
import com.pubsys.main.server.rpc.SisWebRemoteServiceServlet;
import com.pubsys.main.shared.exceptions.AuthenticationException;
import com.pubsys.main.shared.model.Autor;

public class LoginServiceImpl extends SisWebRemoteServiceServlet implements
    LoginService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -744332051812878002L;

	
	
	@Override
	public void login(String mail, String senha) throws AuthenticationException {
		try {
			ResultSet res = Database.getDatabaseInstance().doSelect("select * from \"autor\" where \"email\"='"  + mail +  "' and \"senha\"='" + senha + "'");

			Autor autor = null;
			if (res.next()){
				autor = new Autor();
				autor.setNome(res.getString("nome"));
				autor.setEmail(res.getString("email"));
				autor.setId(res.getInt("id"));
			}
			
			this.setaUsuarioLogado(autor);
		} catch (SQLException e) {
			throw new AuthenticationException("Usuário ou senha incorretos");
		}	
	}


	@Override
	public Autor pegaUsuarioLogado() {
		return this.getLoggedUser();
	}


	@Override
	public void logout() {
		this.setaUsuarioLogado(null);
	}

}